// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "$$root$$.h"

#include "MainFrm.h"
#include "TabClassDoc.h"
#include "TabClassView.h"
#include "TabFileDoc.h"
#include "TabFileView.h"
#include "TabResourceDoc.h"
#include "TabResourceView.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	ON_COMMAND_EX(ID_VIEW_WORKSPACE, OnBarCheck)
	ON_UPDATE_COMMAND_UI(ID_VIEW_WORKSPACE, OnUpdateControlBarMenu)
	ON_COMMAND_EX(ID_VIEW_OUTPUT, OnBarCheck)
	ON_UPDATE_COMMAND_UI(ID_VIEW_OUTPUT, OnUpdateControlBarMenu)
	ON_COMMAND_EX(ID_VIEW_MEMORY, OnBarCheck)
	ON_UPDATE_COMMAND_UI(ID_VIEW_MEMORY, OnUpdateControlBarMenu)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_COMMAND(ID_INIT_TAB_CLASS, OnInitTabClass)
	ON_COMMAND(ID_INIT_TAB_FILE, OnInitTabFile)
	ON_COMMAND(ID_INIT_TAB_RSRC, OnInitTabRsrc)
	ON_WM_CREATE()
	ON_WM_CLOSE()
	ON_COMMAND(ID_TOGGLEFLAT, OnToggleflat)
	ON_UPDATE_COMMAND_UI(ID_TOGGLEFLAT, OnUpdateToggleflat)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_bFlat = FALSE;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	if (!m_wndToolBar.Create(this) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndMenuBar.Create(this) ||
		!m_wndMenuBar.LoadMenu(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Remove this if you don't want tool tips or a resizeable toolbar
	m_wndToolBar.SetBarStyle(m_wndToolBar.GetBarStyle() |
		CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);
	m_wndMenuBar.SetBarStyle(m_wndMenuBar.GetBarStyle() |
		CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC);

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	m_wndMenuBar.EnableDocking(CBRS_ALIGN_ANY);
	m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	EnableDocking(CBRS_ALIGN_ANY);
	DockControlBar(&m_wndMenuBar);
	DockControlBar(&m_wndToolBar);

	// Create the "Workspace" docking window.
	{
		// Initialize dialog bar m_wndWorkspace
		if( !m_wndWorkspace.Create(this, ID_VIEW_WORKSPACE,
			_T("Workspace"), CSize(225,100), CBRS_LEFT ))
		{
			TRACE0("Failed to create dialog bar m_wndWorkspace\n");
			return -1;		// fail to create
		}

		// Create the document for each of the tab views, then add the
		// document / view to the tab control. This is a very simple implementation.
		// you could go further and actually create a document template for each tab,
		// and add it to the app. If so, you would pass in m_pMyDocTemplate->CreateNewDocument(),
		// instead of dynamically allocating a pointer for each document as shown here.
		// This was done for simplicity.

		// class tab
		m_pDocClass = new CTabClassDoc;
		ASSERT_KINDOF( CDocument, m_pDocClass );
		m_wndWorkspace.AddView( _T( "Class" ), RUNTIME_CLASS( CTabClassView ), m_pDocClass );
		// let the document update the tab view.
		m_pDocClass->UpdateTabView();

		// resource tab
		m_pDocResource = new CTabResourceDoc;
		ASSERT_KINDOF( CDocument, m_pDocResource );
		m_wndWorkspace.AddView( _T( "Resource" ), RUNTIME_CLASS( CTabResourceView ), m_pDocResource );
		// let the document update the tab view.
		m_pDocResource->UpdateTabView();
		
		// file tab
		m_pDocFile = new CTabFileDoc;
		ASSERT_KINDOF( CDocument, m_pDocFile );
		m_wndWorkspace.AddView( _T( "Files" ), RUNTIME_CLASS( CTabFileView ), m_pDocFile );
		// let the document update the tab view.
		m_pDocFile->UpdateTabView();

		// Define the image list to use with the tab control
		m_TabImages.Create( IDB_IL_TAB, 16, 1, RGB( 0,255,0 ));
		m_wndWorkspace.SetTabImageList( &m_TabImages );

		m_wndWorkspace.EnableDockingOnSizeBar( CBRS_ALIGN_ANY );
		EnableDockingSizeBar( CBRS_ALIGN_ANY );
		DockSizeBar( &m_wndWorkspace );

		// set the popoup menu id.
		m_wndWorkspace.SetMenuID( IDR_MAINFRAME, NULL, 2 );
	}

	// create the output docking window.
	{
		if( !m_wndOutput.Create(this, ID_VIEW_OUTPUT,
			_T("Output"), CSize(300,150), CBRS_BOTTOM ))
		{
			TRACE0("Failed to create dialog bar m_wndOutput\n");
			return -1;		// fail to create
		}

		m_wndOutput.EnableDockingOnSizeBar( CBRS_ALIGN_ANY );
		EnableDockingSizeBar( CBRS_ALIGN_ANY );
		DockSizeBar( &m_wndOutput );

		// set the popoup menu id.
		m_wndOutput.SetMenuID( IDR_MAINFRAME, NULL, 2 );
	}

	// create the memory docking window.
	{
		if( !m_wndMemory.Create(this, ID_VIEW_MEMORY,
			_T("Memory"), CSize(50, 150), CBRS_BOTTOM ))
		{
			TRACE0("Failed to create dialog bar m_wndMemory\n");
			return -1;		// fail to create
		}

		m_wndMemory.EnableDockingOnSizeBar( CBRS_ALIGN_ANY );
		EnableDockingSizeBar( CBRS_ALIGN_ANY );
		DockSizeBar( &m_wndMemory );

		// set the popoup menu id.
		m_wndMemory.SetMenuID( IDR_MAINFRAME, NULL, 2 );
	}

	LoadBarState(_T("Bar State"));
	return 0;
}

void CMainFrame::OnInitTabClass() 
{
	m_pDocClass->UpdateTabView();
}

void CMainFrame::OnInitTabFile() 
{
	m_pDocFile->UpdateTabView();
}

void CMainFrame::OnInitTabRsrc() 
{
	m_pDocResource->UpdateTabView();
}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;

	cs.style = WS_OVERLAPPED|WS_CAPTION|WS_THICKFRAME|WS_SYSMENU|
		WS_MINIMIZEBOX|WS_MAXIMIZEBOX|WS_MAXIMIZE;

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnClose() 
{
	SaveBarState(_T("Bar State"));
	CFrameWnd::OnClose();
}

BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) 
{
	// TODO: Add your specialized code here and/or call the base class
	
	return CFrameWnd::OnCreateClient(lpcs, pContext);
}

void CMainFrame::OnToggleflat() 
{
	m_bFlat = !m_bFlat;
	m_wndWorkspace.EnableFlatLook(m_bFlat);
	m_wndOutput.EnableFlatLook(m_bFlat);
	m_wndMemory.EnableFlatLook(m_bFlat);
}

void CMainFrame::OnUpdateToggleflat(CCmdUI* pCmdUI) 
{
	pCmdUI->SetCheck(m_bFlat);
}
